class BinaryHeap(object):
    def __init__(self):
        self.heap_list = [0]
        self.current_size = 0

    def insert(self, item):
        self.heap_list.append(item)
        self.current_size += 1
        self.__process_up(self.current_size)

    def __process_up(self, i):
        while i // 2 > 0:
            if self.heap_list[i // 2] > self.heap_list[i]:
                temp = self.heap_list[i // 2]
                self.heap_list[i // 2] = self.heap_list[i]
                self.heap_list[i] = temp

            i = i // 2

    def find_min(self):
        return self.heap_list[1]

    def del_min(self):
        item = self.heap_list[1]
        self.heap_list[1] = self.heap_list.pop()
        self.__process_down(1)

        return item

    def __process_down(self, i):
        while 2 * i <= self.current_size:
            mc = self.__get_min_child(i)

            if self.heap_list[mc] < self.heap_list[i]:
                temp = self.heap_list[mc]
                self.heap_list[mc] = self.heap_list[i]
                self.heap_list[i] = temp

            i = mc

    def __get_min_child(self, i):
        if 2 * i == self.current_size:
            return 2 * i
        else:
            if self.heap_list[2 * i] > self.heap_list[2 * i + 1]:
                return 2 * i + 1
            else:
                return 2 * i

    def is_empty(self):
        return self.heap_list == [0]

    def size(self):
        return self.current_size

    def build(self, alist):
        i = len(alist) // 2
        self.current_size = len(alist)
        self.heap_list = [0] + alist
        while i > 0:
            self.__process_down(i)
            i = i - 1


heap = BinaryHeap()
ex = [9, 6, 5, 2, 3]
heap.build(ex)
print(heap.heap_list)

heap.insert(7)
print(heap.heap_list)

print(heap.del_min())
print(heap.heap_list)
